﻿/**
 * ...
 * @author Lucas Duroj
 */

package getengine.core.event;

class EventManager 
{
	//Singelton definition
	static public var instance (getInstance, null): EventManager;
	static private function getInstance() : EventManager 
	{
		if (instance == null) instance = new EventManager();
		return instance;
	}

	private function new()
	{
		init();
	}
	
	private function init():Void
	{
		
	}
	
	public function dispatchEvent(dispatcher : IEventDispatcher, eventType : String, async : Bool) : Void 
	{
		dispatcher.resivers.get(eventType);
		
		if (!async) {
			_dispatchEvent(dispatcher, eventType);
		} else {
			
		}
	}
	
	public function addEventListener(dispatcher : IEventDispatcher, eventType : String, func : Void->Void ) : Void 
	{
		if (dispatcher.resivers == null) dispatcher.resivers = new Hash < Array < Void->Void >> ();
		if (!dispatcher.resivers.exists(eventType)) dispatcher.resivers.set(eventType , new Array < Void->Void >());
		dispatcher.resivers.get(eventType).push(func);
	}
	
	//Dispatches all waiting async event
	public function flushAsyncEvents() 
	{
		
	}
	
	inline private function _dispatchEvent(dispatcher : IEventDispatcher, eventType : String):Void
	{
		for (func in dispatcher.resivers.get(eventType) ) {
			func();
		}
	}
	
}